package com.wangtao.firstapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.adapters.CommentsAdapter;
import com.wangtao.firstapp.custom.FullyLinearLayoutManager;
import com.wangtao.firstapp.modles.CommentsListData;
import com.wangtao.firstapp.utils.HttpUtils;

/**
 * 评论页面  加载全部评论
 * Created by lanouhn on 16/9/13.
 */
public class CommentsActivity extends Activity implements View.OnClickListener {

    private ImageButton back;
    private RecyclerView comments_RecyclerView;
    private LinearLayout lltSpeak, lltView;
    private TextView cancel, send, content;

    //实体类对象
    private CommentsListData commentsListData;

    //评论适配器
    private CommentsAdapter commentsAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_comments);
        initView();
        getDataFromWeb();
        initEvent();
    }

    private void initView() {
        back = (ImageButton) findViewById(R.id.back_act_comments);//左上角返回
        comments_RecyclerView = (RecyclerView) findViewById(R.id.comments_ScRecyclerView_act_comments);//列表
        lltSpeak = (LinearLayout) findViewById(R.id.lltSpeak_act_comments);//放置“我来说两句”的容器
        lltView = (LinearLayout) findViewById(R.id.lltView_act_comments);//弹出框
        cancel = (TextView) findViewById(R.id.cancel_act_comments);//取消
        send = (TextView) findViewById(R.id.send_act_comments);//发送
        content = (TextView) findViewById(R.id.content_act_comments);//输入框“我来说两句”

    }

    private void getDataFromWeb() {
        Intent intent = getIntent();
        String comments_ID = intent.getStringExtra("comments_iD");

        String urlComments = "http://open4.bantangapp.com/post/comment/list";
        String paramsComments = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&page=0&pagesize=10&id=" + comments_ID;
        try {
            HttpUtils.doPostAsyn(urlComments, paramsComments, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson_comments = new Gson();
                    commentsListData = gson_comments.fromJson(result, CommentsListData.class);
                    if (commentsListData.getData().size() > 0) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    break;
                case 1:
                    commentsAdapter = new CommentsAdapter(CommentsActivity.this, commentsListData);
                    comments_RecyclerView.setAdapter(commentsAdapter);
                    comments_RecyclerView.setLayoutManager(new FullyLinearLayoutManager(CommentsActivity.this));
                    break;
            }
        }
    };

    private void initEvent() {
        back.setOnClickListener(this);
        lltSpeak.setOnClickListener(this);
        cancel.setOnClickListener(this);
        send.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            //右上角的返回
            case R.id.back_act_comments:
                finish();
                break;

            //我来说两句
            case R.id.lltSpeak_act_comments:
                lltView.setVisibility(View.VISIBLE);
                content.setFocusable(true);
                lltSpeak.setVisibility(View.GONE);
                break;

            //取消
            case R.id.cancel_act_comments:
                lltView.setVisibility(View.GONE);
                lltSpeak.setVisibility(View.VISIBLE);
            break;

            //发送，现在写成 取消
            case R.id.send_act_comments:
                lltView.setVisibility(View.GONE);
                lltSpeak.setVisibility(View.VISIBLE);
            break;
        }
    }
}
